package com.jthinker.web.controller.front;

import com.jthinker.bean.enu.Operator;
import com.jthinker.bean.vo.Condition;
import com.jthinker.bean.vo.QueryResult;
import com.jthinker.bean.vo.SearchFilter;
import com.jthinker.service.CategoryService;
import com.jthinker.service.PostService;
import com.jthinker.web.controller.common.BaseController;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import java.util.Map;

/**
 * User: Gang Chen
 * Date: 2015/5/12 21:24
 */
@RequestMapping("/category")
public class CategoryController extends BaseController
{
    @Resource
    private PostService postService;

    @Resource
    private CategoryService categoryService;

    @RequestMapping("/{category}")
    public String view(@PathVariable("category") String category, HttpServletRequest request)
    {
        Long categoryId = categoryService.findBySlug(category);
        SearchFilter filter = getSearchFilter(request);
        filter.getWhere().add(new Condition("category_id", String.valueOf(categoryId), Operator.EQUAL));
        QueryResult<Map<String, Object>> result = postService.queryList(filter);

        request.setAttribute("result", result);
        return "index/index";
    }
}
